//// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
//package com.linqingying.gitcode.pullrequest.data
//
//import com.intellij.diff.editor.DiffEditorTabFilesManager
//import com.intellij.diff.editor.DiffVirtualFileBase
//import com.intellij.openapi.application.TransactionGuard
//import com.intellij.openapi.application.TransactionGuardImpl
//import com.intellij.openapi.fileEditor.FileEditorManager
//import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx
//import com.intellij.openapi.project.Project
//import com.intellij.util.containers.ContainerUtil
//import com.linqingying.gitcode.api.GCRepositoryCoordinates
//import com.linqingying.gitcode.api.data.pullrequest.GCPullRequestShort
//import com.linqingying.gitcode.pullrequest.GCNewPRDiffVirtualFile
//import com.linqingying.gitcode.pullrequest.GCPRDiffVirtualFile
//import com.linqingying.gitcode.pullrequest.GCPRStatisticsCollector
//import com.linqingying.gitcode.pullrequest.GCPRTimelineVirtualFile
//import java.util.concurrent.atomic.AtomicReference
//
//internal class GCPRFilesManagerImpl(private val project: Project,
//                                    private val repository: GCRepositoryCoordinates) : GCPRFilesManager {
//
//  // current time should be enough to distinguish the manager between launches
//  override val id: String = System.currentTimeMillis().toString()
//
//  private val files = ContainerUtil.createWeakValueMap<GCPRIdentifier, GCPRTimelineVirtualFile>()
//  private val diffFiles = ContainerUtil.createWeakValueMap<GCPRIdentifier, DiffVirtualFileBase>()
//  private var newPRDiffFile: AtomicReference<DiffVirtualFileBase?> = AtomicReference()
//
//  override fun createOrGetNewPRDiffFile(): DiffVirtualFileBase {
//    return newPRDiffFile.updateAndGet {
//      it ?: GCNewPRDiffVirtualFile(id, project, repository)
//    }!!
//  }
//
//  override fun createAndOpenTimelineFile(pullRequest: GCPRIdentifier, requestFocus: Boolean) {
//    files.getOrPut(pullRequest) {
//      GCPRTimelineVirtualFile(id, project, repository, pullRequest)
//    }.let {
//      FileEditorManager.getInstance(project).openFile(it, requestFocus)
//      GCPRStatisticsCollector.logTimelineOpened(project)
//    }
//  }
//
//  override fun createAndOpenDiffFile(pullRequest: GCPRIdentifier, requestFocus: Boolean) {
//    diffFiles.getOrPut(pullRequest) {
//      GCPRDiffVirtualFile(id, project, repository, pullRequest)
//    }.let {
//      DiffEditorTabFilesManager.getInstance(project).showDiffFile(it, requestFocus)
//      GCPRStatisticsCollector.logDiffOpened(project)
//    }
//  }
//
//  override fun findTimelineFile(pullRequest: GCPRIdentifier): GCPRTimelineVirtualFile? = files[pullRequest]
//
//  override fun findDiffFile(pullRequest: GCPRIdentifier): DiffVirtualFileBase? = diffFiles[pullRequest]
//
//  override fun updateTimelineFilePresentation(details: GCPullRequestShort) {
//    findTimelineFile(details.prId)?.let {
//      FileEditorManagerEx.getInstanceEx(project).updateFilePresentation(it)
//    }
//  }
//
//  override fun dispose() {
//    if (project.isDisposed) return
//    val fileManager = FileEditorManager.getInstance(project)
//    // otherwise the exception is thrown when removing an editor tab
//    (TransactionGuard.getInstance() as TransactionGuardImpl).performUserActivity {
//      for (file in (files.values + diffFiles.values)) {
//        fileManager.closeFile(file)
//      }
//      newPRDiffFile.get()?.also {
//        fileManager.closeFile(it)
//      }
//      }
//  }
//}
